package com.yukino.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.yukino.pojo.Progress;
import com.yukino.pojo.Project;
import com.yukino.service.ProgressService;
import com.yukino.service.UserService;
import com.yukino.util.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class ProgressController {
    @Autowired
    private UserService userService;

    @Autowired
    private ProgressService progressService;

    /**
     * 查询当前用户参与的项目
     */
    @GetMapping("/progress/list")
    public AjaxJson queryProgress() {
        String loginId = (String) StpUtil.getLoginId();
        List<Project> projects = userService.querySpecificProject(loginId);
        return AjaxJson.getSuccessData(projects);
    }

    /**
     * 查询特定项目的细节
     * url匹配采用restful风格
     * @param project_id 项目id
     */
    @GetMapping("/progress/detail/{project_id}")
    public AjaxJson queryDetail(@PathVariable("project_id") Long project_id) {
        List<Progress> progresses = progressService.queryProgress(project_id);
        return AjaxJson.getSuccessData(progresses);
    }

    /**
     * 提交进度
     * @param project_id        项目id
     * @param commit_name       提交者
     * @param commit_remark     提交备注
     */
    @PutMapping("/progress/add/{project_id}/{commit_name}/{commit_remark}")
    public AjaxJson addOne(@PathVariable("project_id") Long project_id,
                           @PathVariable("commit_name") String commit_name,
                           @PathVariable("commit_remark") String commit_remark) {
        progressService.addOne(project_id, commit_name, commit_remark);
        return AjaxJson.getSuccess();
    }
}
